#include "widget.h"
#include "ui_widget.h"
#include<QTimer>
#include<QKeyEvent>
#include<QToolButton>
#include "bullet.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setFixedSize(512,768);

    mGameView.setSceneRect(QRect(0,0,512,768));
    mScene.setSceneRect(QRect(0,0,512,768));//游戏进行场景
    mStartScene.setSceneRect(QRect(0,0,512,768));
    mStartScene.addPixmap(QPixmap(":/Image/Image/2978cb64b1852b9dce3f7311ecd7d2f.jpg"));

    auto startBtn = new QToolButton();
    startBtn->setAutoRaise(true);
  //  startBtn->resize(171,51);//开始游戏图标大小
    startBtn->setIcon(QIcon(":/Image/Image/91d2df23a7039eaed5854daaf5f2305.jpg"));//开始游戏图标
    startBtn->setIconSize(QSize(171,51));
    startBtn->move(256-171/2,384);
    connect(startBtn,&QToolButton::clicked,[this](){
        mGameView.setScene(&mScene);
        mGameView.show();
    });
    mStartScene.addWidget(startBtn);


    mbackGround1.setPixmap(QPixmap("://Image/img_bg_level_1.jpg"));//与QImage用法类似
    mbackGround2.setPixmap(QPixmap("://Image/img_bg_level_1.jpg"));
    mPlane.setPixmap(QPixmap("://Image/hero2.png"));

    //设置元素位置
     mPlane.setPos(100,100);
     mbackGround2.setPos(0,-mbackGround2.pixmap().height());
    //图片元素添加到场景
     mScene.addItem(&mbackGround1);
     mScene.addItem(&mbackGround2);


     mScene.addItem(&mPlane);
    // mScene.addWidget(new QPushButton());//场景可以添加 文本 图形 矩形 图片
     //设置视图场景
     //mGameView.setScene(&mScene);
     mGameView.setScene(&mStartScene);

     //设置视图的父亲为窗口
     mGameView.setParent(this);
     mGameView.show();



     //开启背景移动定时器
     mBGMoveTimer=new QTimer(this);
     mBGMoveTimer->start(10);
     connect(mBGMoveTimer,&QTimer::timeout,this,&Widget::BGMove);


     //开启飞机移动检测定时器
     mPlaneMoveTimer = new QTimer(this);
     mPlaneMoveTimer->start(10);
     connect(mPlaneMoveTimer,&QTimer::timeout,this,&PlaneMove);

     //开启发射定时器
     mPlaneShootTimer = new QTimer(this);
     mPlaneShootTimer->start(mPlane.mShootSpeed);
     connect(mPlaneShootTimer,&QTimer::timeout,this,&Widget::PlaneBulletShoot);

     //开启子弹移动定时器
     mBulletMoveTimer = new QTimer(this);
     mBulletMoveTimer->start(10);
     connect(mBulletMoveTimer,&QTimer::timeout,[this](){
         for(auto bullet : mBulletList)
         {
             bullet->BulletMove();
         }
     });

     //开启敌机创建
     mEnemyCreateTimer = new QTimer(this);
     mEnemyCreateTimer -> start(3000);
     connect(mEnemyCreateTimer,&QTimer::timeout,this,&Widget::CreateEnemy);

     //开启敌人移动定时器
     mEnemyMoveTimer = new QTimer(this);
     mEnemyMoveTimer -> start(25);
     connect(mEnemyMoveTimer,&QTimer::timeout,[this](){
           for(auto enemy : mEnemyList)
           {
               enemy->EnemyMove();
           }
     });

}

Widget::~Widget()
{
    delete ui;
}

void Widget::BGMove()
{
    mbackGround2.moveBy(0,2);
    mbackGround1.moveBy(0,2);//moveBy 移动量 dx--x方向移动量 dy--y方向移动量

    if(mbackGround1.y() >=mbackGround1.pixmap().height())
    {
        mbackGround1.setY(-mbackGround1.pixmap().height());
    }
    else if(mbackGround2.y() >=mbackGround1.pixmap().height())
    {
        mbackGround2.setY(-mbackGround2.pixmap().height());
    }
}

void Widget::PlaneMove()
{
    for(int keyCode : mKeyList)
    {
        switch(keyCode)
        {
        case Qt::Key_W: mPlane.moveBy(0,-1*mPlane.mMoveSpeed);break;
        case Qt::Key_S: mPlane.moveBy(0,1*mPlane.mMoveSpeed);break;
        case Qt::Key_A: mPlane.moveBy(-1*mPlane.mMoveSpeed,0);break;
        case Qt::Key_D: mPlane.moveBy(1*mPlane.mMoveSpeed,0);break;
        }
    }
    //边界判断
    if(mPlane.x() < 0)
    {
        mPlane.setX(0);
    }
    if(mPlane.y()< 0)
    {
        mPlane.setY(0);
    }
    if(mPlane.x() > this->width() - mPlane.pixmap().width())
    {
        mPlane.setX(this->width() - mPlane.pixmap().width());
    }
    if(mPlane.y() > this->height() - mPlane.pixmap().height())
    {
        mPlane.setY(this->height() - mPlane.pixmap().height());
    }



}

void Widget::keyPressEvent(QKeyEvent *event)
{
    //添加按键到按键组合
    switch(event->key())
    {
        case Qt::Key_W:
        case Qt::Key_S:
        case Qt::Key_A:
        case Qt::Key_D:
        mKeyList.append(event->key());
        break;

    }
}

void Widget::keyReleaseEvent(QKeyEvent *event)
{
    //移除对应按键组合
    if(mKeyList.contains(event->key()))
    {
        mKeyList.removeOne(event->key());
    }

}

void Widget::PlaneBulletShoot()
{
    //创建子弹
   QPixmap bulletImg("://Image/hero_bullet_7.png");
   QPoint pos(mPlane.x()+mPlane.pixmap().width()/2 ,mPlane.y());
   Bullet* bullet =new Bullet(pos,bulletImg,Bullet::BT_Player);

   //添加到场景
   mScene.addItem(bullet);

   //添加到子弹管理器
   mBulletList.append(bullet);
}

void Widget::CreateEnemy()
{
    QPixmap pixmap("://Image/img-plane_5.png");
    int randx = qrand()%(512-pixmap.width());//敌机随机出现
    Enemy* enemy =new Enemy(QPoint(randx,-200),pixmap);

    //添加到场景
    mScene.addItem(enemy);

    //添加到管理器
    mEnemyList.append(enemy);
}
